package cn.exrick.xboot.modules.transport.controller;

import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.transport.entity.CompanyRoutes;
import cn.exrick.xboot.modules.transport.entity.CompanyWarehouses;
import cn.exrick.xboot.modules.transport.mapper.CompanyWarehousesMapper;
import cn.exrick.xboot.modules.transport.vo.CompanyRoutesVo;
import cn.exrick.xboot.modules.transport.param.QueryCompanyRoutesParam;
import cn.exrick.xboot.modules.transport.param.EditCompanyRoutesParam;
import cn.exrick.xboot.modules.transport.service.CompanyRoutesService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author gj
 */
@Slf4j
@RestController
@Api(tags = "物流公司线路信息管理接口")
@RequestMapping("/xboot/companyRoutes")
@Transactional
public class CompanyRoutesController {

    @Autowired
    private CompanyRoutesService companyRoutesService;
    @Autowired
    private CompanyWarehousesMapper companyWarehousesMapper;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<CompanyRoutesVo> get(@PathVariable Long id) {

        return new ResultUtil<CompanyRoutesVo>().setData(companyRoutesService.getRoutesById(id));
    }


    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<CompanyRoutesVo> saveOrUpdate(@RequestBody EditCompanyRoutesParam editCompanyRoutesParam) {
        //通过起点和目的地id,和公司id查询出对应的仓库id，存入companyRoutes中如果为查到对应的仓库则返回错误信息，先查询出对应仓点实体在判断
        CompanyWarehouses originWarehouse = companyWarehousesMapper.selectOne(new LambdaQueryWrapper<CompanyWarehouses>().eq(CompanyWarehouses::getCompanyId, editCompanyRoutesParam.getCompanyId()).eq(CompanyWarehouses::getRegionId, editCompanyRoutesParam.getOriginRegionId()));
        if (originWarehouse == null) {
            return new ResultUtil<CompanyRoutesVo>().setErrorMsg("起点仓库不存在");
        }
        CompanyWarehouses destinationWarehouse = companyWarehousesMapper.selectOne(new LambdaQueryWrapper<CompanyWarehouses>().eq(CompanyWarehouses::getCompanyId, editCompanyRoutesParam.getCompanyId()).eq(CompanyWarehouses::getRegionId, editCompanyRoutesParam.getDestinationRegionId()));
        if (destinationWarehouse == null) {
            return new ResultUtil<CompanyRoutesVo>().setErrorMsg("目的地仓库不存在");
        }
        CompanyRoutes companyRoutes = BeanConverterUtil.convertBean(editCompanyRoutesParam, CompanyRoutes.class);
        companyRoutes.setOriginWarehouseId(originWarehouse.getId());
        companyRoutes.setDestinationWarehouseId(destinationWarehouse.getId());
        if (companyRoutesService.saveOrUpdate(companyRoutes)) {
            return new ResultUtil<CompanyRoutesVo>().setData(BeanConverterUtil.convertBean(editCompanyRoutesParam, CompanyRoutesVo.class));
        }
        return new ResultUtil<CompanyRoutesVo>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestBody Long[] ids) {

        for (Long id : ids) {
            companyRoutesService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<CompanyRoutesVo>> getByCondition(QueryCompanyRoutesParam param,
                                                         PageVo page){
        return new ResultUtil<IPage<CompanyRoutesVo>>().setData(companyRoutesService.getByCondition(PageUtil.initMpPage(page),param));
    }
}
